package hust.ioic.oa.view.action;

import hust.ioic.oa.base.BaseAction;
import hust.ioic.oa.domain.Department;
import hust.ioic.oa.domain.Function;
import hust.ioic.oa.domain.Role;
import hust.ioic.oa.domain.Operator;
import hust.ioic.oa.qilin.utils.GetDate;
import hust.ioic.oa.utils.DepartmentUtils;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class OperatorAction extends BaseAction<Operator> {
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	GetDate cdate = new GetDate();

	private Integer roleId;
	private Integer departmentId;
	private String newpassword;

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		// 从页面拿到水司名称，将数据源设置为相应的数据源
		List<Operator> operatorList = operatorService.findAllByStatus();
		ActionContext.getContext().put("operatorList", operatorList);
		return "list";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		// 从页面拿到水司名称，将数据源设置为相应的数据源
		Operator operator = operatorService.getById(model.getId());
		operator.setStatus(0);
		operatorService.update(operator);
		return "toList";
	}

	/**
	 * 编辑页面
	 * 
	 * @return
	 */
	public String editUI() {
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		List<Department> departmentList = departmentService.findAll();
		ActionContext.getContext().put("departmentList", departmentList);
		// 回显数据
		Operator operator = operatorService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(operator);
		if (operator.getRole() != null) {
			Role role = operator.getRole();
			roleId = role.getId();
		}
		if (operator.getDepartment() != null) {
			Department department = operator.getDepartment();
			departmentId = department.getId();
		}
		return "saveUI";
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {

		Operator operator = operatorService.getById(model.getId());
		operator.setUsername(model.getUsername());
		operator.setGender(model.getGender());
		operator.setRole(roleService.getById(roleId));
		operator.setDepartment(departmentService.getById(departmentId));
		operatorService.update(operator);

		return "toList";
	}

	/**
	 * 添加页面
	 * 
	 * @return
	 */
	public String addUI() {
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtils
				.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		List<String> operatorNames = operatorService.getAllOperatorNames();
		ActionContext.getContext().put("operatorNames", operatorNames);
		// 准备数据, roleList
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "saveUI";
	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {

		// 从页面拿到水司名称，将数据源设置为相应的数据源
		model.setRole(roleService.getById(roleId));
		model.setIsAdmin(0);
		model.setStatus(1);
		// 设置默认密码为1234+水司编号
		String md5Digest = DigestUtils.md5Hex("1234");

		model.setPassword(md5Digest);
		operatorService.save(model);
		return "toList";

	}

	public String changePassword() {
		// 从页面拿到水司名称，将数据源设置为相应的数据源

		Operator operator = operatorService.getById(model.getId());
		String md5Digest = DigestUtils.md5Hex(newpassword);
		operator.setPassword(md5Digest);
		operatorService.update(operator);

		return "right";
	}

	public String changePasswordUI() {

		return "changePasswordUI";
	}

	/**
	 * 初始化密码
	 * 
	 * @return
	 */
	public String initPassword() {
		// 从页面拿到水司名称，将数据源设置为相应的数据源

		Operator operator = operatorService.getById(model.getId());

		String md5Digest = DigestUtils.md5Hex("1234");
		operator.setPassword(md5Digest);
		operatorService.update(operator);

		return "toList";
	}

	/**
	 * 登录页面 用户访问该登录界面的时候，需要查询主数据库，取出所有水司的名字 登录的时候将masterDb数据库的信息读到一个并发容器并放到应用域中
	 * 
	 * @return
	 */
	public String loginUI() {
		return "loginUI";
	}

	/**
	 * 登录
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String login() throws UnsupportedEncodingException {

		Operator operator = operatorService.findByUsernameAndPassowrd(
				model.getUsername(), model.getPassword());
		if (operator != null) {
			ActionContext.getContext().getSession().put("operator", operator);
			// 2011-12-30添加，添加记住我的功能，记住当前操作的用户名和密码 begin
			return "toIndex";
		} else {
			addFieldError("login", "用户名或密码不正确！");
			ActionContext.getContext().getSession().put("operator", operator);
			return "loginUI";

		}
	}

	/**
	 * 首次登录之后，设置页面的功能权限
	 * 
	 * @return
	 */

	/**
	 * 注销
	 * 
	 * @return
	 */
	public String logout() {
		ActionContext.getContext().getSession().remove("operator");
		return "loginUI";
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

}
